package com.kkb.controller.ordermanager;

import com.github.pagehelper.PageInfo;
import com.kkb.bean.BillTable;
import com.kkb.bean.User;
import com.kkb.bean.WithdrawalTable;
import com.kkb.service.billmanager.BillManagerService;
import com.kkb.service.withdrawalmanager.WithdrawalManagerService;
import com.kkb.vo.BaseResp;
import com.kkb.vo.profitmanager.req.Overview;
import com.kkb.vo.profitmanager.req.SelectBillReq;
import com.kkb.vo.profitmanager.req.SelectOverviewReq;
import com.kkb.vo.profitmanager.req.SelectWithdrawalReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/profit")
public class ProfitManagerController {

    @Autowired
    private BillManagerService billManagerService;

    @Autowired
    private WithdrawalManagerService withdrawalManagerService;

    @GetMapping("/queryBill")
    public BaseResp<Map<String,Object>> queryBill(SelectBillReq param, HttpServletRequest request){
        if (param.getPageNum()==null){
            param.setPageNum(1);
        }
        //TODO 这里从session取信息需要和user模块对接
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        param.setSellerId(user.getUserId());
        PageInfo<BillTable> billTablePageInfo = billManagerService.selectBill(param);
        Map<String, Object> map = new HashMap<>();
        map.put("billTablePageInfo",billTablePageInfo);
        map.put("param",param);
        return new BaseResp<>(map);
    }

    @GetMapping("/queryWithdrawal")
    public BaseResp<Map<String,Object>> queryWithdrawal(SelectWithdrawalReq param, HttpServletRequest request){
        if (param.getPageNum()==null){
            param.setPageNum(1);
        }
        //TODO 这里从session取信息需要和user模块对接
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        param.setSellerId(user.getUserId());
        PageInfo<WithdrawalTable> withdrawalTablePageInfo = withdrawalManagerService.selectWithdrawal(param);
        Map<String, Object> map = new HashMap<>();
        map.put("withdrawalTablePageInfo",withdrawalTablePageInfo);
        map.put("param",param);
        return new BaseResp<>(map);
    }

    @GetMapping("/getOverview")
    public BaseResp<Map<String,Object>> getOverview(SelectOverviewReq param, HttpServletRequest request){
        //TODO 这里从session取信息需要和user模块对接
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        param.setSellerId(user.getUserId());
        Overview overview = new Overview();
        //佣金、分成、文库激励、推广激励
        BigDecimal commission = billManagerService.selectCommission(param);
        if (commission==null) commission=new BigDecimal(0);
        BigDecimal bonus = billManagerService.selectBonus(param);
        if (bonus==null) bonus=new BigDecimal(0);
        BigDecimal wenkuExcitation = billManagerService.selectWenkuExcitation(param);
        if (wenkuExcitation==null) wenkuExcitation=new BigDecimal(0);
        BigDecimal adExcitation = billManagerService.selectAdExcitation(param);
        if (adExcitation==null) adExcitation=new BigDecimal(0);
        overview.setCommission(commission);
        overview.setBonus(bonus);
        overview.setWenkuExcitation(wenkuExcitation);
        overview.setAdExcitation(adExcitation);
        //累计收入
        BigDecimal totalIncome = commission.add(bonus.add(wenkuExcitation.add(adExcitation)));
        overview.setTotalIncome(totalIncome);
        //待结算金额
        BigDecimal unsettledMoney = billManagerService.selectMoneyByTime(param.getSellerId());
        if (unsettledMoney==null) unsettledMoney = new BigDecimal(0);
        overview.setUnsettledMoney(unsettledMoney);
        //已提现金额
        BigDecimal withdrawnAmount = withdrawalManagerService.selectWithdrawnAmount(param);
        if (withdrawnAmount==null) withdrawnAmount = new BigDecimal(0);
        //可提现金额=累计收入-待结算金额-已提现金额
        BigDecimal withdrawableMoney = totalIncome.subtract(unsettledMoney.add(withdrawnAmount));
        overview.setWithdrawableMoney(withdrawableMoney);
        Map<String, Object> map = new HashMap<>();
        map.put("overview",overview);
        map.put("param",param);
        return new BaseResp<>(map);
    }



}
